//
//  YZMBranchListViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/20.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMBranchListViewController.h"
#import "UIViewController+Utils.h"
#import "YZMSeparatorCell.h"
#import <LinqToObjectiveC/LinqToObjectiveC.h>
#import <PinYin4Objc/PinYin4Objc.h>

@interface YZMBranchListViewController()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSMutableArray *searchDataSource;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation YZMBranchListViewController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.title = @"选择支行";
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    [self createObjects];
    [self registerForKeyboardAutoWithBottomView:self.bottomView andGestureFlag:NO];
}

#pragma mark -- tableview delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"BankCardCell";
    YZMSeparatorCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YZMSeparatorCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textColor = [YZMAppStyle colorForMainTextColor];
        cell.textLabel.font = [YZMAppStyle mediumSizeFont];
    }
    
    cell.textLabel.text = _searchDataSource[indexPath.section][0];
    cell.textLabel.numberOfLines = 0;
    
    [cell.contentView insertSubview:cell.textLabel belowSubview:cell.bottomLineView];
    YZMIndexCount indexCount;
    indexCount.index = indexPath.section;
    indexCount.count = self.dataSource.count;
    cell.indexCount = indexCount;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [_searchDataSource count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_searchBar resignFirstResponder];
    
    if (self.delegate) {
        
        [self.delegate branchDidChoose:_searchDataSource[indexPath.section][0]];
    }
    
    [YZMHCMInstance popViewController:nil animated:YES];
}

#pragma mark -- searchbar delegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    [searchBar resignFirstResponder];
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    NSString *searchString = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
    
    if (searchString.length == 0) {
        
        [_searchDataSource removeAllObjects];
        [_searchDataSource addObjectsFromArray:_dataSource];
        [_tableView reloadData];
        
        return;
    }
    
    [_searchDataSource removeAllObjects];
    
    for (NSArray *array in _dataSource) {
        
        NSString *a = array[0];
        NSString *b = array[1];
        NSString *c = array[2];
        NSString *d = array[3];
        
        if ( [a containsString:searchString] || [b containsString:searchString] || [c containsString:searchString] || [d containsString:searchString]) {
            
            [_searchDataSource addObject:array];
        }
    }
    
    [_tableView reloadData];
}


#pragma mark -- ui

- (void)createObjects{
    
    self.searchBar = [UISearchBar new];
    self.searchBar.placeholder = @"输入关键字可快速搜索";
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.searchBar.keyboardType =  UIKeyboardTypeDefault;
    self.searchBar.returnKeyType = UIReturnKeyDone;
    self.searchBar.delegate = self;
    for (UIView *view in self.searchBar.subviews) {
        
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0){
            
            [[view.subviews objectAtIndex:0] removeFromSuperview];
        }
    }
    
    [self.view addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.left.right.equalTo(self.view);
        maker.height.mas_equalTo(44);
    }];
    
    self.bottomView = [UIView new];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.bottom.equalTo(self.view);
        maker.height.mas_equalTo(0);
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(self.searchBar.mas_bottom).offset(2);
        maker.left.right.equalTo(self.view);
        maker.bottom.equalTo(_bottomView.mas_top).offset(-2);
    }];
}

#pragma mark -- Params Manager

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMBranchListViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

- (void)updateWithParams:(NSDictionary *)params{
    
    self.delegate = params[@"delegate"];
    NSArray *data = params[@"branchList"];
    
    self.dataSource = [data linq_select:^id(NSString *string){
        
        HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
        outputFormat.toneType = ToneTypeWithoutTone;
        outputFormat.vCharType = VCharTypeWithV;
        outputFormat.caseType = CaseTypeLowercase;
        
        NSString *a =  [PinyinHelper toHanyuPinyinStringWithNSString:string withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
        
        __block NSString *b = [NSString string];
        
        NSArray *wordList = [a componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
        [wordList linq_select:^id(NSString *string){
            
            if (string.length > 0) {
                
                b = [b stringByAppendingString: [string substringToIndex:1]];
            }
            
            return nil;
        }];
        
        NSString *c = [a stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        return @[string, a, b, c];
        
    }];
    
    self.searchDataSource = [NSMutableArray arrayWithArray:self.dataSource];
}


@end
